// Code updated on 7th december 2006
//this servlet creates a new nym, stores the associated nym,email and password - is called by register.js
//package NetTrust;
import java.sql.*;
import java.util.Date;
import java.io.*;
import java.util.*;
import java.util.HashMap;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;

public class CreateNymServlet extends HttpServlet{

	//Initialize the servlet	
	public void init(ServletConfig config)
	{
	  System.out.println("FriendsServlet: init()");
	}
	
	public void doGet(HttpServletRequest request,HttpServletResponse response)    
		throws ServletException, IOException 
	{
		String strPrevNym=null;
		// set the response type
		response.setContentType("text/xml; charset=UTF-8"); 
		response.setHeader("Cache-Control","no-cache");
		String strNym=request.getParameter("nym");
                String strpwd = request.getParameter("pwd");
                String stremail = request.getParameter("email");
		//String strNym="Shreyas_Home";
		int success=this.CreateNym(strNym,strpwd,stremail);
		// obtain the writer to write the response
		PrintWriter out = response.getWriter();
		out.println("<?xml version=\"1.0\"?>");  
		out.println("<Success>");
		out.println("<valid>");
		out.println(success);
		out.println("</valid>");
		out.println("</Success>");
	}
		
		
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// Connect()
	// Connects to the database
	// return value of type Connection
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	public static Connection Connect()
	{
		Connection conn = null;
		
		try
		{
			//Defines the Query statements
			//ResultSet rsBuddy,rsInternal,rsNymID;
			//Connect to the database
			String userName = "nettrust";
			String password = "nettrust06162006";
			String url= "jdbc:mysql://localhost/NetTrust";
			Class.forName ("com.mysql.jdbc.Driver").newInstance ();
			conn = DriverManager.getConnection (url, userName, password);
			System.out.println ("Database connection established");
		}
		catch( Exception e )
		{
			// Do nothing
			System.out.println("Connection failed");
		};  
		return( conn );
	}
	/* * * * * * * * * * * * * * * * * * * * * * * * * * *h * * */
	// end of function Connect()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	public static int CreateNym(String strNym,String strpwd,String stremail)
	{

	      Connection Conn=Connect();
	       Statement stCreateNym;
               try{
			stCreateNym = Conn.createStatement();
		   	try
				{
					UUID unymid = UUID.randomUUID();
					System.out.println("The id is "+unymid);
					stCreateNym.executeUpdate("insert into nymnamemap values('"+unymid+"','"+strNym+"')");
                                        stCreateNym.executeUpdate("insert into nymdetail values('"+unymid+"','"+stremail+"','"+strpwd+"')");
					return 1;
				}catch(Exception e)
				{
					System.out.println (e);
					return 0;
				}
			}catch(Exception e)
			{
				System.out.println (e);
				return 0;
			}
       	      
	}
	
		
	public void destroy() {
		System.out.println("BroadCasterServlet: destroy()");
	}
}
